-- EKT-YTC700TLAT-04-100C&YMC12842
ioInitial(2, "OUTPUT_PP", "NOPULL")
ioInitial(3, "OUTPUT_PP", "NOPULL")
ioInitial(4, "OUTPUT_PP", "NOPULL")
ioInitial(5, "OUTPUT_PP", "NOPULL")
ioInitial(14, "OUTPUT_PP", "NOPULL")
ioInitial(15, "OUTPUT_PP", "NOPULL")
ioInitial(16, "OUTPUT_PP", "NOPULL")

function csL(state)
	ioWrite(14, state)
end 

function csR(state)
	ioWrite(15, state)
end 

function rst(state)
	ioWrite(16, state)
end 

function dc(state)
	ioWrite(2, state)
end

function sda(state)
	ioWrite(4, state)
end

function scl(state)
	ioWrite(5, state)
end

function write_com(byte)
	dc(0)
	for i = 0, 7, 1 do
		scl(0)
		sda(byte & 0x80)
		byte = byte << 1
		scl(1)
	end
end

--serial 4-SPI写数据；
function write_dat(byte)
	--rs = 1
	dc(1)
	--[[for i = 0, 7, 1 do
		scl(0)
		sda(byte & 0x80)
		byte = byte << 1
		scl(1)
	end]]
	scl(0)
	sda(byte & 0x80)
	scl(1)

	scl(0)
	sda(byte & 0x40)
	scl(1)

	scl(0)
	sda(byte & 0x20)
	scl(1)

	scl(0)
	sda(byte & 0x10)
	scl(1)

	scl(0)
	sda(byte & 0x08)
	scl(1)

	scl(0)
	sda(byte & 0x04)
	scl(1)

	scl(0)
	sda(byte & 0x02)
	scl(1)

	scl(0)
	sda(byte & 0x01)
	scl(1)
end


--走横程序i为页地址这里是  0xb0~0xb8, j为seg数，这里是128,循环时修改起始行即可
function lcd_line()
	--for i = 0xb0 i < 0xb6 i++) 
	for i = 0xb0, 0xb5, 1 do 
		write_com(i)
		write_com(0x10)
		write_com(0x00)
		for j = 0, 127, 1 do
			write_dat(0x88)
		end
	end
end

--清屏程序，由走横程序改编而成，送数 0x00
function lcd_clear()
	for i = 0xb0, 0xb5, 1 do
		write_com(i)
		write_com(0x10)
		write_com(0x00)
		for j = 0, 127, 1 do
			write_dat(0x00)
		end
	end
end

function lcd_full()
	for i = 0xb0, 0xb5, 1 do 
		write_com(i)
		write_com(0x10)
		write_com(0x00)
		for j = 0, 127, 1 do
			write_dat(0xff)
		end
	end
end

--走竖程序，
function lcd_erect(byte1, byte2, byte3, byte4)
	for i = 0xb0, 0xb5, 1 do 
		write_com(i)
		write_com(0x10)
		write_com(0x00)
		for j = 0, 128/4-1, 1 do
			write_dat(byte1)
			write_dat(byte2)
			write_dat(byte3)
			write_dat(byte4)
		end
	end
end

--/*--  ??x??=128x48  --*/
image_yes1 = {
	0xFF,0x01,0x01,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0x01,0x01,0x01,0xFF,
	0xFF,0x00,0x00,0x00,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD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xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,
	0xE1,0xC1,0x81,0x01,0xFF,0xFD,0xF9,0xF1,0xE1,0xC1,0x81,0x01,0x00,0x00,0x00,0xFF,
	0x03,0x02,0x02,0x02,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,
	0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x02,0x02,0x02,0x03,

}

--图形显示
function lcd_image(p)
	--[[print("lcd_image(p)\r\n")

	for k, v in pairs(p) do 
		print(v.." ")
	end ]]
	--[[
	for i = 0, 5, 1 do 
		print("ram addr: "..(i+0xb0).."\r\n")
		write_com(i+0xb0)	 -- RAM page address
		write_com(0x10) -- RAM column address most address
		write_com(0x00) -- RAM column address least address
		for j = 0, 127, 1 do
			print(" "..p[i*128+j])
			write_dat(p[i*128+j])
		end
	end]]
	--write_com(0xb0)	 -- RAM page address
	--write_com(0x10) -- RAM column address most address
	--write_com(0x00) -- RAM column address least address

	for k, v in pairs(p) do 
		--print(k.." "..v.."\r")
		if(k%128 == 1)
		then
			write_com(0xb0 + (k-1)/128)	 -- RAM page address
			write_com(0x10) -- RAM column address most address
			write_com(0x00) -- RAM column address least address
		end
		write_dat(v)
	end
end


function test12842()
	csL(0)
	csR(0)
	rst(0)
	delay_ms(100)
	rst(1)

	write_com(0xe2)     --软件复位
	write_com(0xa2)     -- 1/8bias
	write_com(0xa0)     -- adc direction: normal
	write_com(0xc8)     -- com direction: reverse
	write_com(0x26)     -- Ra/Rb
	write_com(0x81)     -- set v0
	write_com(24) -- 11v
	write_com(0x2c)     -- vc,vr,vf
	write_com(0x2e)
	write_com(0x2f)
	write_com(0x40) -- RAM display start line address
	write_com(0xaf) -- display on

	write_com(0xa4) -- display normal


	csL(0)
	csR(0)
	lcd_clear()
	delay_ms(200)
	--检测CS脚状态
	csL(1)
	csR(1)
	--lcd_image(image_yes2)
	delay_ms(500)
	csL(0)
	csR(0)

	lcd_full() -- display full
	delay_ms(500)

	lcd_image(image_yes1) --图形
	delay_ms(500)

	write_com(0x43) -- 改变起始行实现走横循环
	lcd_line()
	delay_ms(500)
	write_com(0x42)
	delay_ms(500)
	write_com(0x41)
	delay_ms(500)
	write_com(0x40)
	delay_ms(500)

	lcd_erect(0xff, 0x00, 0x00, 0x00)
	delay_ms(500) -- 走竖循环
	lcd_erect(0x00, 0xff, 0x00, 0x00)
	delay_ms(500)
	lcd_erect(0x00, 0x00, 0xff, 0x00)
	delay_ms(500)
	lcd_erect(0x00, 0x00, 0x00, 0xff)
	delay_ms(500)

end

print("MONO LCD Display Start\r\n")
test12842()
print("MONO LCD Display Finish\r\n")

t10_DPI(1024, 100, 30, 30, 600, 18, 3, 3, 0, 0, 0, 1, 50)

t10_setInterval(200)
